<?php
$token = base64_encode(json_encode([
    'lang' => $lang,
    'env' => 'replay',
    'gameId' => $data['game_id'],
    'nonce' => Str::random(96),
]));
$params = base64_encode(json_encode([
    'gameId' => $data['game_id'],
    'result' => $detail['details']['result'] ?? [],
]));
$lang_slo = trans('view.game.data.settlement.slo');
$lang_boolean = trans('view.form.booleanList');
?>

<div style="position: relative; overflow: hidden; padding-top: 56.25%;">
    <iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0 none;" id="playback"></iframe>
</div>

<div class="row" style="margin-top: 33px;">
    @isset($data['ext']['spin_type'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['spin_type'] }}</label>
                <div class="el-form-item__content">{{ $lang_slo['types'][$data['ext']['spin_type']] ?? $data['ext']['spin_type'] }}</div>
            </div>
        </div>
    @endisset
    @isset($data['ext']['is_free'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['is_free'] }}</label>
                <div class="el-form-item__content">{{ $data['ext']['is_bonus'] == 1 ? $lang_boolean[0] : ($lang_boolean[$data['ext']['is_free']] ?? $data['ext']['is_free']) }}</div>
{{--                <div class="el-form-item__content">{{ $lang_boolean[$data['ext']['is_free']] ?? $data['ext']['is_free'] }}</div>--}}
            </div>
        </div>
    @endisset
    @isset($data['ext']['is_free_trigger'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['is_free_trigger'] }}</label>
                <div class="el-form-item__content">{{ $lang_boolean[$data['ext']['is_free_trigger']] ?? $data['ext']['is_free_trigger'] }}</div>
            </div>
        </div>
    @endisset
    @isset($data['ext']['free_trigger_source'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['free_trigger_source'] }}</label>
                <div class="el-form-item__content">{{ $lang_slo['freeTriggerSourceList'][$data['ext']['free_trigger_source']] ?? $data['ext']['free_trigger_source'] }}</div>
            </div>
        </div>
    @endisset
    @isset($data['ext']['is_bonus'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['is_bonus'] }}</label>
                <div class="el-form-item__content">{{ $lang_boolean[$data['ext']['is_bonus']] ?? $data['ext']['is_bonus'] }}</div>
            </div>
        </div>
    @endisset
    @isset($data['ext']['is_bonus_trigger'])
        <div class="col">
            <div class="el-form-item">
                <label class="el-form-item__label">{{ $lang_slo['ext']['is_bonus_trigger'] }}</label>
                <div class="el-form-item__content">{{ $lang_boolean[$data['ext']['is_bonus_trigger']] ?? $data['ext']['is_bonus_trigger'] }}</div>
            </div>
        </div>
    @endisset

</div>

<script>
    function encodeGameToken(token) {
        if (token.length < 96) {
            throw 'Game Token Error'
        }
        let secretKey = randomString(4);
        for (let i = 0; i < secretKey.length; i++) {
            token = insertStr(token, 23 * (i + 1) + 3, secretKey[i]);
        }
        return token;
    }

    function randomString(length) {
        let str = 'abcdefghijklmnpqrstuvwxyz';
        const maxPos = str.length;
        let code = '';
        for (let i = 0; i < length; i++) {
            const txt = str.charAt(Math.floor(Math.random() * maxPos));
            code += txt;
        }
        return code;
    }

    function insertStr(source, start, newStr) {
        return source.slice(0, start) + newStr + source.slice(start);
    }

    let iframe = document.getElementById("playback");
    iframe.src = "{{ $domain['info']['cdnClientDomain'][0] }}?token=" + encodeGameToken("{{ $token }}") + "&lang={{$lang}}";
    window.addEventListener('message', function (e) {
        iframe.contentWindow.postMessage(encodeGameToken("{{ $params }}"), '*');
    });
</script>
